package com.aceitunaproject.mapdisplay;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import com.aceitunaproject.mapdisplay.model.Thing;
import com.aceitunaproject.mapdisplay.model.map.tile.Tile;


public class KeyListenerImpl implements KeyListener {

	public void keyPressed(KeyEvent e) {


	}

	private Thing theUnit;
	private int x = 1;
	private int y = 1;
	private int dir = 0;
	private int ydir = 0;
	
	public void keyReleased(KeyEvent e) {

		Tile currentTile = PruebaInternalFrame.theFrame.map.getTile(x, y);

		if (theUnit == null)
			theUnit = PruebaInternalFrame.theFrame.map.getTile(x, y)
					.getFirstThing();

		
		
		
		switch (e.getKeyCode()) {
		case KeyEvent.VK_LEFT:
			dir = -1;
			break;
		case KeyEvent.VK_RIGHT:
			dir = 1;
			break;
		case KeyEvent.VK_UP:
			ydir = -1;
			break;
		case KeyEvent.VK_DOWN:
			ydir = 1;
			break;
		default:
			break;
		}
		
//		if (x == 5 && dir == 1) {
//			dir = -1;
//
//		} else {
//			if (x == 1)
//				dir = 1;
//		}
		x = x + dir;
		y = y + ydir;
		Tile newTile = PruebaInternalFrame.theFrame.map.getTile(x, y);
		if(newTile != null)
		{
			currentTile.removeThing(theUnit);
			newTile.addThing(theUnit);
		}
		else
		{
			x = x - dir;
			y = y - ydir;
		}
		PruebaInternalFrame.theFrame.dp.repaint();

	}

	
	public void keyTyped(KeyEvent e) {


	}

}
